# -*- coding: utf-8 -*-
import scrapy


class QimingSpider(scrapy.Spider):
    name = 'qiming'
    allowed_domains = ['5156edu.com','threetong.com']
    start_urls = ['http://xh.5156edu.com/xm/nu.html']

    def parse(self, response):
        last_name = '张'
        # with open("qiming.html","wb")as fp:
        #     fp.write(response.body)
        name_list = response.xpath('//a[@class="fontbox"]/text()').extract()
        # 所有的字
        print(name_list)

        two_name_list = []
        #设置一个名字列表
        for name in name_list:
            for name2 in name_list:
                two_name = name+name2
                two_name_list.append(two_name)
        name_list =  name_list + two_name_list
        # 找出所有2 个字的组合
        print(len(name_list))

        for name in name_list:
            form = {
                'isbz': '1',
                'txtName': last_name,
                'name': name,
                'rdoSex': '0',
                'data_type': '0',
                'cboYear': '2025',
                'cboMonth': '7',
                'cboDay': '29',
                'cboHour': '9 - 巳时',
                'cboMinute': '1分',
                'pid': '北京',
                'cid': '选择城市',
                'zty': '0',

            }
            url = 'https://www.threetong.com/ceming/baziceming/xingmingceshi.php'
            #get 方法 scarapy.Request
            # post 方法  scrapy.FormRequest
            req = scrapy.FormRequest(url=url,formdata=form,callback=self.check_name)
            req.meta['wanted_name'] = last_name + name
            yield req


    def check_name(self,response):
        lishu_score = response.xpath('//div[@class="sm_pingfen"]/span[1]/text()').extract_first()
        bazi_score = response.xpath('//div[@class="sm_pingfen"]/span[2]/text()').extract_first()
        # 使用replace处理多出来的  只要数字
        lishu_score = lishu_score.replace('姓名理数评分:', '')
        bazi_score = bazi_score.replace('姓名配合八字评分:', '')
        if float(lishu_score) >80.0 and float(bazi_score) > 80.0:
            print('你的名字是 :',response.meta['wanted_name'],'分数是',lishu_score,bazi_score)